﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using Domain.Model;
using Domain.Types;
using DTO;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class ListController : ControllerBase
    {
        public ListApplication listApplication { get; set; }

        public ILogger<ListController> _logger { get; set; }

        /// <summary>
        /// 推荐列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<TaskOutput>> GetRecommentList(int page = 1, int size = 20)
        {
            try
            {
                var uid = User.Claims.FirstOrDefault(i => i.Type == "ID")?.Value;
                int _uid = string.IsNullOrWhiteSpace(uid) ? 0 : int.Parse(uid);

                var list = listApplication.GetRecommentList(_uid, page, size);
                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 任务列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="categoryID">0代表全部</param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<TaskOutput>> GetTaskListByCategory(string keywords, int categoryID = 0, int page = 1, int size = 20)
        {
            try
            {
                var uid = User.Claims.FirstOrDefault(i => i.Type == "ID")?.Value;
                int _uid = string.IsNullOrWhiteSpace(uid) ? 0 : int.Parse(uid);

                var list = listApplication.GetTaskListByCategory(_uid, keywords, categoryID, page, size);


                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }


        /// <summary>
        /// 任务详情
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<TaskDetailsOutput> GetTaskDetails(int taskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                int _uid = string.IsNullOrWhiteSpace(uid) ? 0 : int.Parse(uid);

                var task = listApplication.GetTaskDetails(_uid, taskID);

                return this.Success(task);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取我领取的任务列表
        /// </summary>
        /// <param name="status">不传获取全部,待提交 = 0, 审核中 = 1, 审核通过 = 2, 审核未通过 = 3, 关闭 = 4</param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet, Authorize]
        public ActionResult GetTakeTaskList(TaskTakeAuditStatus? status, int page, int size)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var list = listApplication.GetTakeTaskList(int.Parse(uid), status, page, size);
                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 我发布的任务列表
        /// </summary>
        /// <param name="status">不传全部，待发布 = 0, 展示中 = 1, 已经完成 = 2</param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet, Authorize]
        public ActionResult GetMyTaskList(int? status, int page, int size)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                Domain.Types.TaskStatus[] statusArr = null;

                if (status != null)
                {
                    switch (status)
                    {
                        case 0:
                        default:
                            statusArr = new Domain.Types.TaskStatus[] { Domain.Types.TaskStatus.待发布 };
                            break;
                        case 1:
                            statusArr = new Domain.Types.TaskStatus[] { Domain.Types.TaskStatus.进行中, Domain.Types.TaskStatus.暂停,
                            Domain.Types.TaskStatus.任务待关闭, Domain.Types.TaskStatus.任务待完成 };
                            break;
                        case 2:
                            statusArr = new Domain.Types.TaskStatus[] { Domain.Types.TaskStatus.任务关闭, Domain.Types.TaskStatus.任务完成 };
                            break;
                    }
                }


                var list = listApplication.GetMyTaskList(int.Parse(uid), statusArr, page, size);
                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取系统黑名单
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetBlackList(int page, int size)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var list = listApplication.GetBlackList(page, size);
                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 用户排行榜
        /// </summary>
        /// <param name="type">接单榜：0 ，推广榜：1</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<Domain.Model.RewardListAggregate>> GetUserRewardList(RewardListType type)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var list = listApplication.GetUserRewardList(type);

                return this.Success(list, hasMore: false);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取客服问答列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<KFQAAggregate>> GetKfqaList()
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var list = listApplication.GetUserRewardList();

                return this.Success(list, hasMore: false);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }


       /// <summary>
       /// 我的任务审核列表
       /// </summary>
       /// <param name="status"></param>
       /// <param name="page"></param>
       /// <param name="size"></param>
       /// <returns></returns>
        [HttpGet]
        public ActionResult<List<TaskTakeListOutput>> GetTaskTakeList(int TaskID,TaskTakeAuditStatus? status, int page = 1, int size = 20)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var list = listApplication.GetTaskTakeList(TaskID,int.Parse(uid), status, page, size);
                return this.Success(list, hasMore: list.Count >= size);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
    }
}
